package com.huang.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.auth.constants.AuthorityWrapperConstant;
import com.huang.auth.constants.RoleAuthorityWrapperConstant;
import com.huang.auth.converter.AuthResourceSimpleVOConverter;
import com.huang.auth.dao.AuthResourceDao;
import com.huang.auth.dao.AuthRoleAuthorityDao;
import com.huang.auth.entity.po.AuthResourcePO;
import com.huang.auth.entity.po.AuthRoleAuthorityPO;
import com.huang.auth.entity.request.AuthResourceEditRequest;
import com.huang.auth.entity.vo.AuthResourceSimpleVO;
import com.huang.auth.service.AuthResourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 资源(AuthResource)表服务实现类
 *
 * @author HuangShen
 * @since 2021-09-21 17:14:01
 */
@Service("authResourceService")
public class AuthResourceServiceImpl extends ServiceImpl<AuthResourceDao, AuthResourcePO> implements AuthResourceService {

    @Resource
    private AuthResourceDao authResourceDao;

    @Resource
    private AuthRoleAuthorityDao authRoleAuthorityDao;

    @Resource
    private AuthResourceSimpleVOConverter authResourceSimpleVOConverter;


    /**
     * 修改权限资源
     *
     * @param authResourceEditRequest authResourceEditRequest
     * @return Boolean
     */
    @Override
    public Boolean resourceEdit(AuthResourceEditRequest authResourceEditRequest) {

        AuthResourcePO authResourcePO = authResourceDao.selectById(authResourceEditRequest.getId());
        if (Objects.isNull(authResourcePO)) {
            return Boolean.FALSE;
        }
        authResourcePO.setUrl(authResourceEditRequest.getUrl());
        authResourcePO.setMethod(authResourceEditRequest.getMethod());
        authResourcePO.setDescribe(authResourceEditRequest.getDescribe_());
        authResourcePO.setUpdateUser(Long.getLong(authResourceEditRequest.getUpdateUser()));
        authResourcePO.setName(authResourceEditRequest.getName());
        authResourcePO.setCode(authResourceEditRequest.getCode());
        authResourcePO.setMenuId(authResourceEditRequest.getMenuId());

        return authResourceDao.updateById(authResourcePO) > 0;
    }

    /**
     * 删除权限资源
     *
     * @param resourceId resourceId
     * @return boolean
     */
    @Override
    @Transactional
    public Boolean resourceDel(Long resourceId) {
        //1. 删除资源
        int resourceDel = this.authResourceDao.deleteById(resourceId);
        //2. 删除资源和其他表的关联映射
        QueryWrapper<AuthRoleAuthorityPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(RoleAuthorityWrapperConstant.AUTHORITY_ID, resourceId);
        int roleAuthorityDel = this.authRoleAuthorityDao.delete(queryWrapper);
        return (resourceDel > 0 && roleAuthorityDel > 0);
    }

    @Override
    public List<AuthResourceSimpleVO> menuResourceSimpleList(String menuId) {
        QueryWrapper<AuthResourcePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(AuthorityWrapperConstant.ID, AuthorityWrapperConstant.CODE, AuthorityWrapperConstant.NAME, AuthorityWrapperConstant.DESCRIBE,AuthorityWrapperConstant.MENU_ID);

        queryWrapper.isNull(AuthorityWrapperConstant.MENU_ID);
        if (!StringUtils.isEmpty(menuId)){
            queryWrapper.or().eq(AuthorityWrapperConstant.MENU_ID, menuId);
        }

        List<AuthResourcePO> authResourcePOS = this.authResourceDao.selectList(queryWrapper);
        return authResourceSimpleVOConverter.batchConvert(authResourcePOS);
    }
}

